<script type="text/javascript">
	function insert_kaos($diskon){
		var id_produk = $("#id_produk").val();
		var ukuran = $("#ukuran").val();
		var jml = $("#jumlah").val();
		var dataString = 'id_produk='+ id_produk +'&ukuran='+ ukuran +'&jumlah=' + jml +'&diskon=' +$diskon;
		$.ajax({
			type: "POST",
			url: "ajax/insert_cart.php",
			data: dataString,
			cache: false,
			success: function(html){
				loadAjax('ajax/cart.php','cart');
			} 
		});
	}

	function insert_cart($id,$diskon){
		var id_produk = $id;
		var jml = $("#jumlah").val();
		var dataString = 'id_produk='+ id_produk +'&jumlah=' + jml+'&diskon=' +$diskon;
		$.ajax({
			type: "POST",
			url: "ajax/insert_cart.php",
			data: dataString,
			cache: false,
			success: function(html){
				loadAjax('ajax/cart.php','cart');
			} 
		});
	}
</script>

<script>
	$(document).ready(function(){
		$("#ukuran").change(function(){
			var id_produk = $("#id_produk").val();
			var id_ukuran = $("#ukuran").val();
			$.ajax({
				type: "POST",
				url: "ajax/cek_stok.php",
				data: "id_produk="+id_produk+"&id_ukuran="+id_ukuran,
				cache: false,
				success: function(msg){
					$("#stok").html(msg);
				}
			});
		});
	});
</script>

<div class="col-md-9">
  <!-- random product -->
  <?php
		$absolute_url = full_url($_SERVER);
		$slug = str_replace("/BOS/produk-","","$absolute_url");

		$sttmt = $koneksi->query("select * from produk where slug = '$slug'");
		$hasil = $sttmt->fetch(PDO::FETCH_ASSOC);
		$id = $hasil['id_produk'];
  ?>
  <div class="span12">
	<div class="outer-border nav-page-sidebar-p10">
	  <div class="nav-judul bg-texture margin-bottom">
		<span class="judul-sidebar">
		  Detail Produk
		</span>
	  <div class="clearfix"></div>
	  </div>
	  <div class="row">
		<div class="col-md-5">
		  <div class="img-detail">
			<img src="produk/<?php echo $hasil["gambar"]; ?>" class="img-responsive">
		  <div class="clearfix"></div>
		  </div>
		<div class="clearfix"></div>
		</div>
		<div class="col-md-7">
		  <p class="judul-detail"><?php echo $hasil["nama_produk"]; ?></p>
		  <p class="harga-detail">
			<?php 
				if($_SESSION['id_member'] != 'BOS000' && $hasil['diskon_member'] != 0){
					$harga = $hasil['harga'] * ((100 - $hasil['diskon_member'])/100);
					$diskon = $hasil['diskon_member'];
					echo "<s>".harga($hasil['harga'])."</s> &nbsp; ".harga($harga); 
				}else if($_SESSION['id_member'] == 'BOS000' && $hasil['diskon_umum'] != 0){
					$harga = $hasil['harga'] * ((100 - $hasil['diskon_member'])/100);
					$diskon = $hasil['diskon_umum'];
					echo "<s>".harga($hasil['harga'])."</s> &nbsp; ".harga($harga);
				}else{
					echo harga($hasil['harga']);
				} 
			?>
		  </p>
		  <p>
		  <form class="form-horizontal" role="form">
		  	<?php
				if ($hasil["id_kategori"]==1) {
			?>
  				<div class="form-group">
					<label for="ukuran" class="col-lg-2 control-label small">Ukuran</label>
			    	<div class="col-lg-4">
					   	<select class="form-control input-sm" name="ukuran" id="ukuran">
							<option value=""> -- Ukuran -- </option>
					   		<?php
					   			$sttmt = $koneksi->query("select * from ukuran u inner join stok s on s.id_ukuran = u.id_ukuran inner join produk p on p.id_produk = s.id_produk where u.id_ukuran <> 1 and s.stok <> 0 and p.id_produk = $id");
					   			while ($u = $sttmt->fetch(PDO::FETCH_ASSOC)) {
					   				echo "<option value='$u[id_ukuran]'>$u[ukuran]</option>";
					   			}
					   		?>
						</select>
						<input type="hidden" value="<?php echo "$hasil[id_produk]";?>" id="id_produk">
			    	</div>
			  	</div>
			<?php
				}
			?>
				<div class="form-group">
				    <label for="qty" class="col-lg-2 control-label small">Jumlah</label>
				    <div class="col-lg-4">
				   		<input type="text" class="form-control input-sm" id="jumlah" value="1" maxlength="2">
			   		 </div>
			  	</div>
				<div class="form-group">
				    <label for="qty" class="col-lg-2 control-label small">Stok</label>
				    <div class="col-lg-8">
				   		<label class="control-label small">
							<?php 
								if ($hasil["id_kategori"]==1) {
									echo "<span id=stok>Pilih ukuran untuk melihat stok.</span>";
								}else{
									$stok = $koneksi->query("select stok from stok where id_produk = $id and id_ukuran = 1");
									$s = $stok->Fetch(PDO::FETCH_ASSOC);
									echo "<span id=stok>$s[stok]</span>
									<input type=hidden value=$s[stok] name=stok>";
								}
							?>
						</label>
			   		 </div>
			  	</div>
			</form>
		  </p>
		  <p>
			<?php
				$jumlah = $koneksi->query("select sum(stok) as jumlah from stok where id_produk = $hasil[id_produk]");
				$j = $jumlah->Fetch(PDO::FETCH_ASSOC);
				$jml = number_format($j['jumlah']);
				if($jml > 0){
					if ($hasil["id_kategori"]==1) {
						if($diskon == ''){
							$diskon = 0;
						}
						echo "<button type='button' class='btn btn-success' onClick='insert_kaos($diskon)'>
								Beli &nbsp;
								<span class='glyphicon glyphicon-shopping-cart'></span>
							</button>";
					}else{
						if($diskon == ''){
							$diskon = 0;
						}
						echo "<button type='button' class='btn btn-success' onClick='insert_cart(".$hasil["id_produk"].",$diskon)'>
							Beli &nbsp;
							<span class='glyphicon glyphicon-shopping-cart'></span>
						</button>";
					}
				}else{
					echo "<button type='button' class='btn btn-danger'>
							Stok Habis &nbsp;
							<span class='glyphicon glyphicon-ban-circle'></span>
						</button>";
				}
			?>
		  </p>
		  <!-- mulai img detail small -->
		  <div class="clearfix"></div>
		  <?php 
			$sttmt = $koneksi->query("select * from gambar where id_produk = '$id'");
			$jml = $sttmt->rowCount();
			if($jml > 0){
		  ?>
			  <h5>Gambar lainnya:</h5>
			  <div class="outer-noborder">
				<div class="outer-img-detail-small">
					<?php
						while($g = $sttmt->fetch(PDO::FETCH_ASSOC)){
							echo "<a href='#'><img src='produk/$g[gambar]'></a>";
						}
					?>
				<div class="clearfix"></div>
				</div>
			  <div class="clearfix"></div>
			  </div>
		  <?php
			}
		  ?>
		  <!-- end img detail small -->
		</div>
	  </div>
	  <!-- mulai produk terkait -->
	  <?php 
		$terkait = $koneksi->query("select * from produk where id_anime = '".$hasil["id_anime"]."' and id_produk <> '$id' order by rand() limit 4");
		$jml_terkait = $terkait->rowCount();
		if($jml_terkait > 0){
	  ?>
	  <div class="outer-produk-terkait">
		<div class="nav-judul bg-texture">
		  <span class="judul-sidebar">
			Produk Terkait
		  </span>
		<div class="clearfix"></div>
		</div>
		<div class="outer-noradius nav-page-sidebar-p10">
			<?php
				while($t = $terkait->fetch(PDO::FETCH_ASSOC)){
				?>
				  <a href="<?php echo "produk-".$t['slug']; ?>">
					<div class="produk-terkait-single">
					  <h1><?php echo $t['nama_produk']; ?></h1>
					  <img src="produk/<?php echo $t['gambar']; ?>">
					<div class="clearfix"></div>
					</div>
				  </a>
				<?php
				}
		  ?>
		<div class="clearfix"></div>
		</div>
	  <div class="clearfix"></div>
	  </div>
	  <?php
		}
		if($hasil["deskripsi"] != ''){
	  ?>
	  <!-- end produk terkait -->
	  
	  <div class="outer-ket-produk">
		<ul class="nav nav-tabs">
		  <li class="active"><a href="#">Keterangan Produk</a></li>
		</ul>
		<p class="ket-produk">
		  <?php
			echo $hasil["deskripsi"];
		  ?>
		</p>
	  <div class="clearfix"></div>
	  </div>
	  <?php
		}
	  ?>
	<div class="clearfix"></div>
	</div>
  <div class="clearfix"></div>
  </div>
  <!-- end random product -->

</div>